package com.chain.BrotherExchange.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataSource;
import com.chain.BrotherExchange.utils.Androids;
import com.example.common.widget.state.NetworkStateView;

/**
 * 发送短信验证码
 */
public class SendVerificationCodeDialog extends Dialog implements NetworkStateView {

    private final View mProgressBar;
    private final TextView mErrorMessageText;
    private final EditText mAuthCodeEdit;

    public static void show(
            Context context, View.OnClickListener sendListener,
            DataSource.DataCallback<SendVerificationCodeDialog> callback) {
        new SendVerificationCodeDialog(context, sendListener, callback).show();
    }

    private SendVerificationCodeDialog(
            Context context, View.OnClickListener sendListener,
            DataSource.DataCallback<SendVerificationCodeDialog> callback) {
        super(context);

        View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_send_verification_code, null);
        addContentView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        // 进度
        mProgressBar = contentView.findViewById(R.id.progress_bar);

        // 错误信息
        mErrorMessageText = contentView.findViewById(R.id.error_message);

        // 短信短信
        TextView sendText = contentView.findViewById(R.id.send_text);
        sendText.setOnClickListener(sendListener);

        // 输入的验证码
        mAuthCodeEdit = contentView.findViewById(R.id.auth_code_et);
        initObserve(mAuthCodeEdit);

        // 提交数据
        View affirmButton = contentView.findViewById(R.id.affirm_button);
        affirmButton.setOnClickListener(v -> {
            String code = mAuthCodeEdit.getText().toString();
            if (TextUtils.isEmpty(code)) {
                mErrorMessageText.setText(R.string.message_verification_code_is_empty);
            } else {
                callback.onResult(this);
            }
        });

        // 用户当前手机号码
        String phone = Androids.LocalConfig.getPhone();
        TextView mobilePhoneText = contentView.findViewById(R.id.mobile_phone_text);
        mobilePhoneText.setText(String.format(context.getResources()
                .getString(R.string.current_mobile_phone_number), phone));

        // 关闭对话框
        ImageView closeImage = contentView.findViewById(R.id.close_image);
        closeImage.setOnClickListener(v -> dismiss());
    }

    public String getCode() {
        return mAuthCodeEdit.getText().toString();
    }

    private void initObserve(EditText authCodeEdit) {
        authCodeEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mErrorMessageText.setText(null);
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setBackgroundColor(Color.parseColor("#f7f7f7"));
        getWindow().setAttributes(params);
    }

    @Override
    public void loadingNetwork() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void successNetwork() {
        mProgressBar.setVisibility(View.GONE);
        dismiss();
    }

    @Override
    public void errorNetwork(CharSequence charSequence) {
        mProgressBar.setVisibility(View.GONE);
        mErrorMessageText.setText(charSequence);
    }

    @Override
    public void notNetwork() {

    }

    @Override
    public void emptyNetwork() {

    }

    @Override
    public void onAnewRequestNetwork() {

    }
}
